package com.iweb.newa202104.controller;

import com.iweb.newa202104.enums.Err;
import com.iweb.newa202104.exception.CommonException;
import com.iweb.newa202104.service.DurationService;
import com.iweb.newa202104.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: LGZ
 * @Date: 2021/5/12 16:28
 * @Description:
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/duration")
public class DurationController {

    @Autowired
    DurationService durationService;

    @GetMapping("/getAllByCategory")
    public ResultVO getAllByCategory(String categoryId) {
        if (null == categoryId) {
            throw new CommonException(Err.PRAM_ERR);
        }
        try {
            return durationService.getAllByCategory(Integer.parseInt(categoryId));
        } catch (NumberFormatException e) {
            throw new CommonException(Err.PRAM_ERR);
        }
    }

    @GetMapping("/getAllByResource")
    public ResultVO getAllByResource(String resourceId) {
        if (null == resourceId) {
            throw new CommonException(Err.PRAM_ERR);
        }
        try {
            return durationService.getAllByResource(Integer.parseInt(resourceId));
        } catch (NumberFormatException e) {
            throw new CommonException(Err.PRAM_ERR);
        }
    }

    @GetMapping("/getAll")
    public ResultVO getAll() {
        return durationService.getAll();
    }

    @GetMapping("/getAllByUser")
    public ResultVO getAllByUser(String userId){
        if (null == userId) {
            throw new CommonException(Err.PRAM_ERR);
        }
        try {
            return durationService.getAllByUser(Integer.parseInt(userId));
        } catch (NumberFormatException e) {
            throw new CommonException(Err.PRAM_ERR);
        }
    }
    @GetMapping("/getUserResource")
    public ResultVO getUserResource(String userId,String resourceId){
        if (null == userId || null == resourceId ) {
            throw new CommonException(Err.PRAM_ERR);
        }
        try {
            return durationService.getUserResource(Integer.parseInt(userId),Integer.parseInt(resourceId));
        } catch (NumberFormatException e) {
            throw new CommonException(Err.PRAM_ERR);
        }
    }
    @GetMapping("/getUserCategory")
    public ResultVO getUserCategory(String userId,String categoryId ){
        if (null == userId || null == categoryId ) {
            throw new CommonException(Err.PRAM_ERR);
        }
        try {
            return durationService.getUserResource(Integer.parseInt(userId),Integer.parseInt(categoryId));
        } catch (NumberFormatException e) {
            throw new CommonException(Err.PRAM_ERR);
        }
    }
}
